#!/usr/bin/env python
# -*- coding:utf-8 -*-

'''通过点,面数据创建实体

历史:
    2018-03-17 YY 创建
'''

import bpy
from bpy_extras.object_utils import object_data_add

def add_solid(name, verts, faces, location = None):
    bpy.ops.object.select_all(action = 'DESELECT')

    mesh = bpy.data.meshes.new(name)
    mesh.from_pydata(verts, [], faces)
    if mesh.name != name:
        mesh.name = name

    obj = bpy.data.objects.new(mesh.name, mesh)

    # Link object to scene and make active
    scene = bpy.context.scene
    scene.objects.link(obj)
    scene.objects.active = obj
    scene.update()
    obj.select = True

    bpy.ops.object.mode_set(mode="EDIT")
    bpy.ops.mesh.normals_make_consistent(inside=False)
    bpy.ops.object.mode_set(mode="OBJECT")
    if location:
        obj.location = location
    else:
        bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')




if __name__ == '__main__':
    try:
        bpy.ops.object.mode_set(mode="OBJECT")
    except:
        pass
    verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 0, 1)]
    faces = [(0, 1, 2), (0, 1, 3), (1, 2, 3), (0, 2, 3)]
    add_solid('L,上部,主梁、在（要）,空心板,1-1', verts, faces)